package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.PageHelper;
import com.example.springboot.entity.CheckRoom;
import com.example.springboot.entity.LeaveStu;
import com.example.springboot.entity.Student;
import com.example.springboot.mapper.CheckRoomMapper;
import com.example.springboot.service.CheckRoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CheckRoomServiceImpl extends ServiceImpl<CheckRoomMapper, CheckRoom> implements CheckRoomService {

    @Resource
    private CheckRoomMapper checkRoomMapper;

    @Override
    public int addcheckRoom(CheckRoom checkRoom) {

        checkRoom.setChecktime(new Date());
        return checkRoomMapper.insert(checkRoom);
    }

    @Override
    public PageInfo find(Integer pageNum, Integer pageSize, String search) {
        if ("".equals(search)) {
            search = null;
        }
        List<CheckRoom> checkRoomList = checkRoomMapper.findsql(search, pageNum, pageSize);

        for (CheckRoom checkRoom : checkRoomList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (checkRoom.getChecktime() != null)
                checkRoom.setChecktimestr(simpleDateFormat.format(checkRoom.getChecktime()));
        }

        PageInfo page = PageHelper.pageHelper(checkRoomList, pageNum, pageSize);

        return page;
    }

    @Override
    public int updateCheckRoom(CheckRoom checkRoom) {
        return checkRoomMapper.updateByStudentid(checkRoom);
    }

    @Override
    public int deleteCheckRoom(Integer id) {
        return checkRoomMapper.deleteById(id);
    }

    @Override
    public Integer findcount(Integer pageNum, Integer pageSize, String search) {
        return checkRoomMapper.findcount(search);
    }
}
